/* eslint no-extend-native: ["error", { "exceptions": ["String"] }] */
(() => {
  /**
   * 判断字符串是否为空
   **/
  if (!String.prototype.isNullOrEmpty) {
    String.prototype.isNullOrEmpty = function(this: string): boolean {
      return !(this && this.trim());
    };
  }
  /**
   * 字符串数字时间戳转化为时间
   **/
  if (!String.prototype.parseTime) {
    String.prototype.parseTime = function(pattern?: string): string {
      let time;
      const _this = String(this);
      if (/^[0-9.]+$/.test(_this)) {
        time = Number(_this);
      } else time = this.replace(new RegExp(/-/gm), '/');
      if (!time) return '';
      return new Date(time).format(pattern ? pattern : '');
    };
  }
  /**
   * 字符串数字做相加运算
   **/
  if (!String.prototype.add) {
    String.prototype.add = function(arg2: number): number {
      let r1;
      let r2;
      try {
        r1 = this.toString().split('.')[1].length;
      } catch (e) {
        r1 = 0;
      }
      try {
        r2 = arg2.toString().split('.')[1].length;
      } catch (e) {
        r2 = 0;
      }

      const m = Math.pow(10, Math.max(r1, r2));
      let _t = 0;
      _t = Number(this);
      return Math.round(_t * m + arg2 * m) / m;
    };
  }
  /**
   * 字符串数字做相减运算
   **/
  if (!String.prototype.reduce) {
    String.prototype.reduce = function(arg2: number): number {
      let r1;
      let r2;
      try {
        r1 = this.toString().split('.')[1].length;
      } catch (e) {
        r1 = 0;
      }
      try {
        r2 = arg2.toString().split('.')[1].length;
      } catch (e) {
        r2 = 0;
      }
      const m = Math.pow(10, Math.max(r1, r2));
      let _t = 0;
      _t = Number(this);
      return Math.round(_t * m - arg2 * m) / m;
    };
  }
  /**
   * 得到字符串的长度，包括中文和英文
   **/

  if (!String.prototype.charlen) {
    String.prototype.charlen = function(): number {
      // eslint-disable-next-line no-control-regex
      const arr = this.match(/[^\x00-\xff]/gi);
      return this.length + (arr === null ? 0 : arr.length);
    };
  }
  /**
   * 字符串首尾去空格
   **/
  if (!String.prototype.trim) {
    String.prototype.trim = function() {
      return this.replace(/(^\s*)|(\s*$)/g, '');
    };
  }
  /**
   * 字符串包含字符串判断
   **/
  if (!String.prototype.contains) {
    String.prototype.contains = function(str) {
      return this.indexOf(String(str)) !== -1;
    };
  }
  /**
   * 字符串比较大小
   **/
  if (!String.prototype.compare) {
    String.prototype.compare = function(str) {
      if (!str) {
        return -1;
      }

      if (this.length !== str.length) {
        return this.length - str.length;
      }
      let i = 0;
      for (; i < this.length; i++) {
        const val = this.charCodeAt(i) - str.charCodeAt(i);
        if (val !== 0) {
          return val;
        }
      }

      return 0;
    };
  }
  /**
   * 替换字符方式1
   * 格式化字符串${0} -> 参考printf %s
   **/
  if (!String.prototype.format) {
    String.prototype.format = function(): string {
      // eslint-disable-next-line prefer-rest-params
      const args = arguments;
      return this.replace(/\$\{(\d+)\}/g, function(m, i) {
        return args[i];
      });
    };
  }
  /**
   * 替换字符方式2
   * start开始下标, len长度, replaced替换的字符串
   **/
  if (!String.prototype.replaceLen) {
    String.prototype.replaceLen = function(start, len, replaced): string {
      if (!len) {
        return this as string;
      }
      if (start >= this.length) {
        return this as string;
      }
      let returnSeg = '';
      let returnSeg2 = '';
      let i = 0;
      for (; i < this.length; i++) {
        const c = this.charAt(i);
        if (i < start) {
          returnSeg += c;
        }

        if (i >= start + len) {
          returnSeg2 += c;
        }
      }
      return returnSeg + replaced + returnSeg2;
    };
  }
  /**
   * 替换字符方式3
   * 替换字符，这个在替换填入比较有用，比如***天***小时 替换为 <input />天<input />小时
   **/
  if (!String.prototype.replaceChar) {
    String.prototype.replaceChar = function(target, replaced, start: number): string {
      let _start = start;
      let _target = target;
      if (!_target) {
        return this as string;
      }
      if (!_start) {
        _start = 0;
      }
      let returnVal = this.substring(0, _start);
      let index = 0;
      for (let i = _start; i < this.length; i++) {
        const c = this.charAt(i);
        _target = typeof _target === 'function' ? _target.call(this, index) : _target;
        if (c === _target) {
          returnVal += typeof replaced === 'function' ? replaced.call(this, index) : replaced;
          while (i < this.length - 1 && this.charAt(i + 1) === c) {
            i++;
          }
          index++;
        } else {
          returnVal += c;
        }
      }
      return returnVal;
    };
  }
})();

export {};
